class SongCreditsController < ApplicationController
  #filter_access_to :all
  
  #If AJAX request, use the slim layout (no header or footer)
  #Otherwise use the main layout
  layout Proc.new { |controller| controller.request.xhr? ? 'slim' : 'main' }

  # GET /songs/:song_id/song_credits/new
  def new
    @song = Song.find(params[:song_id])
   
    #If the page is loaded with an artist id, show that
    # artist and allow credit details to be specified
    #if params[:artist_select_id]
    #  @artist = Artist.find(params[:artist_select_id])
    #end

    #Stores that this page is waiting for an artist
    #store_waiting_for

  end
  
  # GET /songs/:song_id/song_credits/search
  def search
	@artists = Artist.search(params[:search])
	
	respond_to do |format|
	  format.html { }
	  format.js {
	    render :update do |page|
		  page.replace_html("song-credits-search-results", :partial => 'results',
		                                                   :locals => { :artists => @artists })
		end
	  }
	end
  end

  # POST /songs/:song_id/song_credits/
  def create
    @song = Song.find(params[:song_id])
    credit = SongCredit.new(params[:song_credit])
    credit.song = @song

    #If an artist id was passed along, set it as the credits artist
	if params[:artist_id]
	  credit.artist_id = params[:artist_id]
	  
	#Otherwise a name was passed, which means create a new artist
	else
	  new_artist = Artist.new(:name => params[:artist_name])
	  credit.artist = new_artist
	end
	
    #Attempt to save
    if credit.save
      flash[:notice] = 'Credit successfully added.'
      redirect_to_request_uri
    else
      flash[:errors] = 'Failed to create credit, please try again.'
      render :new
    end
  end

  # GET /song_credits/:id
  def edit
    @song_credit = SongCredit.find(params[:id])
  end

  # PUT /song_credits/:id
  def update
    @song_credit = SongCredit.find(params[:id])

	respond_to do |format|
      #Update the credit details
      if @song_credit.update_attributes(params[:song_credit])
        flash[:notice] = 'Credit successfully updated.'
        format.html {redirect_to_request_uri}
		format.js   {
		  render :update do |page|
		    page.replace_html("song-credit-#{@song_credit.id}", :partial => "song_credits/song_credit",
			                                                    :locals => { :credit => @song_credit })
		  end
		}
      else
        flash[:errors] = 'Failed to update credit, please try again.'
		format.html { render :edit }
		format.js {
		  render :update do |page|
		    page.replace_html(:errors, @song_credit.errors.full_messages)
		  end
		}
      end
	end
  end

  # DELETE /song_credits/:id
  def destroy
    @credit = SongCredit.find(params[:id])

    #Hack to get around after_delete callback issues
    entry = Hash.new
    entry.store("Removed", [@credit.artist.name, @credit.song.title])
    Edit.create_edit(entry, @credit.artist)

	respond_to do |format|
      #Attempt to delete said credit
      if @credit.destroy
        flash[:notice] = 'Credit successfully removed'
        format.html{redirect_to_request_uri}
		format.js{
		  render :update do |page|
		    page.remove "song-credit-#{params[:id]}"
		  end
		}
      else
        flash[:errors] = 'Credit was not able to be deleted, please try again.'
        redirect_to_request_uri
      end
	end
  end

end
